//
//  BaseViewController.swift
//  ContactPoster
//
//  Created by TSYH on 2023/11/20.
//

import UIKit
import SnapKit

open class BaseViewController: UIViewController {
    private lazy var placeHolderView = PageStatePlaceHolderView()
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        
        if !shouldHideNavigationBar {
            view.addSubview(myNavigationBar)
            myNavigationBar.snp.makeConstraints { make in
                make.top.leading.trailing.equalToSuperview()
            }
            if myNavigationBar.actionHandler == nil {
                myNavigationBar.actionHandler = { [weak self] item in
                    self?.navigationBarClick(item)
                }
            }
        }
    }
    
    open override var title: String? {
        get {
            return myNavigationBar.currentTitle
        }
        set {
            myNavigationBar.updateTitle(newValue, font: UIFont.systemFont(ofSize: 17, weight: .semibold))
        }
    }
    
    open override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if !shouldHideNavigationBar {
            view.bringSubviewToFront(myNavigationBar)
        }
        
        
        #if DEBUG
        print("进入页面------>\(String(describing: type(of: self)))")
        #endif
        
    }
    
    deinit {
        #if DEBUG
        print("销毁页面------>\(String(describing: type(of: self)))")
        #endif
    }
    
    private var normalNavigationBar = BaseNavigationBar()
    open var myNavigationBar: BaseNavigationBar {
        return normalNavigationBar
    }
    
    open var shouldHideNavigationBar: Bool {
        return false
    }
    
    open func reloadView() {}
    
    /// 导航栏按钮点击
    open func navigationBarClick(_ item: BaseNavigationBarItem) {
        
    }
    
    // MARK: Page PlaceHolder - 页面占位
    public func showPlaceHolder(state: PageStatePlaceHolder,
                                actionViews: [UIView] = []) {
        if placeHolderView.superview == nil {
            placeHolderView.isHidden = true
            if !shouldHideNavigationBar {
                view.insertSubview(placeHolderView, belowSubview: myNavigationBar)
            } else {
                view.insertSubview(placeHolderView, at: 1)
            }
            placeHolderView.snp.makeConstraints { make in
                if !shouldHideNavigationBar {
                    make.top.equalTo(myNavigationBar.snp.bottom)
                } else {
                    make.top.equalToSuperview()
                }
                make.leading.trailing.equalToSuperview()
                make.height.equalToSuperview().multipliedBy(0.6)
            }
        }
        
        placeHolderView.isHidden = false
        placeHolderView.set(state: state)
        placeHolderView.updateActionViews(actionViews)
    }
    
    public func hidePlaceHolder() {
        placeHolderView.isHidden = true
    }
    
    /// 退出页面后的需要执行的操作
    open func handlerActionAfterPopFinished() {}
    
    /// 点击返回按钮是否pop
    open var shouldPopWhenClickBackItem: Bool { return true }
    
    /// shouldPopWhenClickBackItem为false时的响应事件
    open func handleNavigationBarBackBlockAction() {}
}

// 转场动画
extension BaseViewController: UIViewControllerTransitioningDelegate {
    open func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if let navVC = presented as? UINavigationController,
           let transitionVC = navVC.topViewController as? TransitioningProtocol {
            return transitionVC.openTransition
        }
        else if let transitionController = presented as? TransitioningProtocol {
            return transitionController.openTransition
        }
        return nil
    }
    
    open func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if let navVC = dismissed as? UINavigationController,
           let transitionVC = navVC.topViewController as? TransitioningProtocol {
            return transitionVC.closeTransition
        }
        else if let transitionController = dismissed as? TransitioningProtocol {
            return transitionController.closeTransition
        }
        return nil
    }
}
